package com.tequila.core.service.impl;

import com.google.common.collect.Lists;
import com.tequila.core.dao.ManagerDao;
import com.tequila.core.service.ManagerService;
import com.tequila.domain.dto.ManagerDto;
import com.tequila.domain.entity.Manager;
import com.tequila.domain.query.ManagerQuery;
import fe.core.Assert;
import fe.core.BeanHelper;
import fe.core.Pagination;
import fe.core.UserDetail;
import fe.core.springmvc.UserDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

public class ManagerServiceImpl implements ManagerService,UserDetailService {
    public static final Logger logger = LoggerFactory.getLogger(ManagerServiceImpl.class);

    @Resource
    private ManagerDao managerDao;

    @Override
    public Pagination<ManagerDto> queryPageListByParam(ManagerQuery query) {
        Assert.assertNotNull(query);
        int total = managerDao.queryPageCountByParam(query);

        List<ManagerDto> list = Lists.newArrayList();
        if (total > 0) {
            list = managerDao.queryPageListByParam(query);
        }
        return new Pagination<>(query, list, total);
    }

    @Override
    public ManagerDto queryById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        Manager manager = managerDao.queryById(id);
        ManagerDto managerDto = new ManagerDto();
        BeanHelper.copyProperties(managerDto,manager);
        return managerDto;
    }

    @Override
    public List<ManagerDto> queryByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return managerDao.queryByIdList(idList);
    }

    @Override
    public List<ManagerDto> queryListByParam(ManagerQuery query) {
        Assert.assertNotNull(query,"query不能为空!");
        return managerDao.queryListByParam(query);
    }


    @Override
    public Long insert(Manager entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        managerDao.insert(entity);
        return entity.getId();
    }

    @Override
    public Long save(Manager entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        managerDao.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public int updateById(Manager param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return managerDao.updateById(param);
    }

    @Override
    public int updateSelectiveById(Manager param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return managerDao.updateSelectiveById(param);
    }

    @Override
    public int deleteById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        return managerDao.deleteById(id);
    }

    @Override
    public int deleteByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return managerDao.deleteByIdList(idList);
    }

    public ManagerDto queryManagerByAccount(String account) {
        Assert.assertNotNull(account,"account不能为空!");
        Manager manager = managerDao.queryByAccount(account);
        ManagerDto managerDto = null;
        if(manager != null) {
            managerDto = new ManagerDto();
            BeanHelper.copyProperties(managerDto,manager);
        }
        return  managerDto;
    }

    @Override
    public UserDetail getUserDetail(String account) {
        Manager manager = managerDao.queryByAccount(account);
        UserDetail userDetail = UserDetail.createUserDetail();
        userDetail.setAccount(manager.getAccount());
        userDetail.setId(manager.getId());
        return userDetail;
    }
}
